package name.bingo.activiti.controller;
 import org.activiti.engine.RepositoryService;
 import org.activiti.engine.RuntimeService;
 import org.activiti.engine.TaskService;
 import org.activiti.engine.repository.Deployment;
 import org.activiti.engine.repository.ProcessDefinition;
 import org.activiti.engine.runtime.ProcessInstance;
 import org.activiti.engine.task.Task;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @param:
 * @return:
 * @auther: yanbing
 * @date: 2018/11/16 12:18
 **/
@RestController
@RequestMapping("activiti")
public class DemoController {
   @Autowired
   private RepositoryService repositoryService;
   @Autowired
   private RuntimeService runtimeService;
   @Autowired
   private TaskService taskService;
    @RequestMapping("/start")
    @ResponseBody
    public String start() {
        Deployment deployment = repositoryService.createDeployment().name("test流程").addClasspathResource("processes/MyProcess.bpmn").deploy();
        System.out.println("流程部署ID："+deployment.getId());
        System.out.println("流程部署时间："+deployment.getDeploymentTime());
        System.out.println("流程部署名称："+deployment.getName());
        System.out.println("流程部署名称："+deployment.getKey());
        String piId = runtimeService.startProcessInstanceByKey("myProcess").getId();
        Task task = taskService.createTaskQuery().processInstanceId(piId).singleResult();
        System.out.println("当前流程节点：" + task.getName());

        return task.getId();
    }

    @RequestMapping("/complete/{taskId}")
    @ResponseBody
    public String complete(@PathVariable String taskId) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String piId = task.getProcessInstanceId();

        taskService.complete(task.getId());

        // 查询当前流程节点
        Task currentTask = taskService.createTaskQuery().processInstanceId(piId).singleResult();
        System.out.println("当前流程节点：" + currentTask.getName());

        return "success";
    }
}
